package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.common.service.AccountSafeService;
import cn.jekun.shop.common.service.RegService;

/**
 * 账户安全设置
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class AccountSafeAction extends AppBaseAction{
	@Resource(name = AccountSafeService.BEAN_NAME)
	protected AccountSafeService accountSafeService;
	@Resource(name = RegService.BEAN_NAME)
	RegService regService;
	/**
	 * 设置登录密码
	 * @return
	 */
	public String resetLoginPwd(){
		JSONObject jsonObject = null;
		try{
			String userId = this.getUserId();
			String newPwd = this.request.getParameter("newPwd");
			String oldPwd = this.request.getParameter("oldPwd");
//			this.validateLoginApp();
//			this.validateSign(userId,newPwd,oldPwd);
			jsonObject = accountSafeService.resetLoginPwd(userId,newPwd,oldPwd);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "设置登录密码成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "设置登录密码失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	public String setMobile(){
		JSONObject jsonObject = null;
		try{
			String userId = this.getUserId();
			String mobile = this.request.getParameter("mobile");
			this.validateLoginApp(); 
			this.validateSign(userId,mobile);
			jsonObject = accountSafeService.setMobile(userId,mobile);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "设置手机号码成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "设置手机号码失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 修改绑定手机
	 * @return
	 */
	public String updateMobile(){
		JSONObject jsonObject = null;
		try{
			String userId = this.getUserId();
			String newMobile = this.request.getParameter("newMobile");
			String oldMobile = this.request.getParameter("oldMobile");
			String tempMobile = this.getSessionUser().getMobile();
			String validateCode = this.request.getParameter("validateCode");//手机验证码
			this.validateLoginApp(); 
			this.validateSign(userId,newMobile,oldMobile,validateCode);
			jsonObject = accountSafeService.updateMobile(userId,newMobile,oldMobile,tempMobile,validateCode);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改绑定手机成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改绑定手机失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 发送验证码
	 * @return
	 */
	public String sendValidateCode(){
		JSONObject jsonObject = null;
		try {
			String newMobile = this.request.getParameter("newMobile");//新手机号
			String oldMobile = this.request.getParameter("oldMobile");//旧手机号
			String tempMobile = this.getSessionUser().getMobile();
			this.validateLoginApp();
			this.validateSign(newMobile,oldMobile);
			jsonObject = accountSafeService.sendBeforValidate(newMobile,oldMobile,tempMobile);
			//如果code==‘1’则发送
			if(jsonObject.get("code").toString().equals("1")){
				jsonObject = regService.sendValidateCodeByUpdateMobile(newMobile,Constants.RS_SMS_TYPE.UPDATE_MOBILE_CODE);
			}
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
